package com.course.server.impl;

import com.course.dto.*;
import com.course.entity.SC;
import com.course.entity.Student;
import com.course.mapper.StuMapper;
import com.course.server.StuService;
import com.course.vo.ChooseCourse;
import com.course.vo.CourseList;
import com.course.vo.StudentCredit;
import com.course.vo.StudentsCourse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@Service
public class StuServiceImpl implements StuService {
    @Autowired
    StuMapper stuMapper;
    /**
     * 添加学生
     */
    public void addstu(StuDTO stuDTO) {
        Student student=new Student();
        //对象属性拷贝
        BeanUtils.copyProperties(stuDTO,student);
        //补全学生信息
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());

        stuMapper.insert(student);

    }

    @Override
    public Student Querystu(StuQueryDTO stuQueryDTO) {
        return null;
    }

    /**
     * 更新学生信息
     * @param student
     */
    @Override
    public void Updatestu(Student student) {

        //设置修改时间
        student.setUpdateTime(LocalDateTime.now());

        stuMapper.update(student);
        System.out.println("update方法执行了");
    }

    /**
     * 根据sno去查询学生
     * @param sno
     * @return
     */
    @Override
    public List<Student> list2(Integer sno) {
        List<Student> studentList=stuMapper.list2(sno);
        return studentList;
    }

    @Override
    public void delete(Integer sno) {
        //调用mapper层去删除学生
        stuMapper.deleteBySNO(sno);
    }

    /**
     * 分页查询插件的使用
     * @param page
     * @param pageSize
     * @param sno
     * @param sname
     * @return
     */
    @Override
    public PageBean page(Integer page, Integer pageSize, Integer sno, String sname) {
        // 正确使用PageHelper的startPage方法，注意这里不需要立即转换类型，PageHelper会自动处理
        PageHelper.startPage(page, pageSize);

        log.info("传入的sname为:{},{}",sno,sname);
        // Mapper方法调用中，=号是用来指定参数名的，实际传参应直接写值，不要包含=
        List<Student> students = stuMapper.list(sno, sname);

        // 通过PageHelper的Page类实例获取分页信息和数据列表
        PageInfo<Student> pageInfo = new PageInfo<>(students);

        // 封装PageBean返回给前端
        PageBean pageBean = new PageBean(pageInfo.getTotal(), pageInfo.getList());
        return pageBean;
    }
    /**
     * 查询学生学分信息
     * @return
     */
    @Override
    public List<StudentCredit> studentCredit(Integer sno) {
        List<StudentCredit> studentCredits=stuMapper.studentCredit(sno);
        return studentCredits;
    }

    @Override
    public void ChooseCourse(ChooseCourse chooseCourse) {
        //设置初始成绩
        SC sc=new SC();
        BeanUtils.copyProperties(chooseCourse,sc);
        sc.setGrade(0);

        stuMapper.ChooseCourse(sc);
    }

    @Override
    public List<StudentsCourse> GetCourse(Integer sno) {
        List<StudentsCourse> GetCourse=stuMapper.GetCourse(sno);
        return GetCourse;

    }

    @Override
    public void deleteByCno(Integer sno,String cno) {
        stuMapper.deleteByCNO(sno,cno);
    }

    @Override
    public Student login(Student student) {
        Student loginStudent=stuMapper.getByUsernameAndPassword(student);
        return loginStudent;
    }

    /**
     * 更新学生课程信息
     * @param sc
     */
    @Override
    public void updateGrade(SC sc) {
        stuMapper.Grade(sc);
    }

    /**
     * 根据学号更新学生密码
     * @param sno
     */
    @Override
    public void updatePassword(Integer sno) {
        stuMapper.updatePassword(sno);
    }

    @Override
    public void definePassword(updatePassword toupdatePassword) {
        stuMapper.definePassword(toupdatePassword);
    }
}

/*@Override
    public PageBean page(Integer page, Integer pageSize,Integer sno,String sname,String ssex,Integer sage) {
        Page<Student> stuPage = PageHelper.startPage(page, pageSize);
        stuPage = (Page<Student>) stuMapper.list(sno=sno, sname=sname, ssex=ssex, sage=sage);
        //封装PageBean返回给前端
        PageBean pageBean=new PageBean(stuPage.getTotal(),stuPage.getResult());
        return pageBean;
    }*/
//设置分页参数
//PageHelper.startPage(page,pageSize);
//执行分页查询
//List<Student> studentList=stuMapper.list(sno,sname);
//获取分页结果
//Page<Student> stu=(Page<Student>) studentList;
